package com.itheima.edu.info.manager.C_Service;

import com.itheima.edu.info.manager.D_Dao.BaseStudentDao;
import com.itheima.edu.info.manager.D_Dao.StudentDao;
import com.itheima.edu.info.manager.E_domain.Student;
import com.itheima.edu.info.manager.Factory_Dao.DaoFactory;

import java.util.ArrayList;

//业务员类
public class StudentService {

    BaseStudentDao studentDao = DaoFactory.getInstance();
//    ArrayList<Student> allStudent = studentDao.findAllStudent();


    //使用add方法将stu对象传给仓管：
    public boolean addStudent(Student student) {
        return studentDao.addStudent(student);
    }

    public boolean isExists(String id) {
        ArrayList<Student> allStudent = studentDao.findAllStudent();
        //找仓管要存储的数据集合继续遍历判断：
        //先假设不存在：
        boolean flag = false;
        for (int i = 0; i < allStudent.size(); i++) {
            if (allStudent.get(i) != null && allStudent.get(i).getId().equals(id)) {
                //如果遍历的集合非空且已存数据的id和输入的id相同，则设置true
                flag = true;
                break;
            }
        }
        return flag;
    }

    public ArrayList<Student> findAllStudent() {
        ArrayList<Student> allStudent = studentDao.findAllStudent();
        return allStudent;
    }

    public void updateStudent(Student student) {

        ArrayList<Student> allStudent = studentDao.findAllStudent();
        for (int i = 0; i < allStudent.size(); i++) {
            if (student.getId().equals(allStudent.get(i).getId())) {
                //如果遍历的集合元素的id与传入的id相等，则更新：
                studentDao.updateStudent(student, i);
            }
        }

    }

    public void deleteStudent(String id) {
        ArrayList<Student> allStudent = studentDao.findAllStudent();
        for (int i = 0; i < allStudent.size(); i++) {
            //如果遍历的集合元素不为空且id与传入的id相等，则删除：
            Student student = allStudent.get(i);
            if (allStudent != null && student != null && id.equals(student.getId())) {
                studentDao.deleteStudent(i);
            }

        }

    }

    public boolean anlayzAge(int age) {
        if (age > 0 && age < 140) {
            return true;
        } else
            return false;
    }
}


